Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

437
Views
How can i add webview in my class? [findViewById(int) in fragment]

I have an activity and i want it to display a website using webview. But i am not able to do it with my existing code.

(EDIT: I made the necessary changes as told by Nirmal Raj but now i am getting an error that "Cannot resolve method findViewById(int)" i have edited my code please look at it and identify the problem.)

BlogAcitivity.java

package thenerdimite.nuttybuddies;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class BlogActivity extends Fragment {

    View myView;
    WebView webView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.activity_blog, container, false);
        return myView;

        webView = (WebView)myView.findViewById(R.id.blogview);

        webView = (WebView)findViewById(R.id.blogview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.google.com");
    }

}

The error "Cannot resolve method findViewById(int)" coming with a red color. What changes should i do in this code so that the webview can work. Thanks!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I am not sure what you want but I will give you a sample of how to implement a webview.

Layout:

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webs"/>

Add this to the layout in which you want to show the WebView.

Java Code:

        webView = (WebView)findViewById(R.id.webs);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.google.com");

Since you are using fragments the only difference will be in findViewById() function. You have to call myView.findViewbyId().

Also don't forget to add this to the manifest

<uses-permission android:name="android.permission.INTERNET"/>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!